package com.shycloud.mido.basic.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.basic.api.dto.DictionaryDTO;
import com.shycloud.mido.basic.api.entity.BasDictionary;
import com.shycloud.mido.basic.service.BasDictionaryService;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 字典管理 前端控制器
 *
 * @author yyy
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dictionary")
@Api(value = "dictionary", description = "字典管理模块")
public class DictionaryController {

	private final BasDictionaryService basDictionaryService;

	/**
	 * 通过ID查询字典信息
	 *
	 * @param id ID
	 * @return 字典信息
	 */
	@GetMapping("/{id}")
	public R getById(@PathVariable Integer id) {
		return new R<>(basDictionaryService.getById(id));
	}


	/**
	 * 分页查询字典信息
	 *
	 * @param page 分页对象
	 * @return 分页对象
	 */
	@GetMapping("/page")
	public R<IPage> getDictPage(Page page, BasDictionary basDictionary) {
		return new R<>(basDictionaryService.page(page, Wrappers.query(basDictionary)));
	}

	/**
	 * 通过字典类型查找字典
	 *
	 * @param type 类型
	 * @return 同类型字典
	 */
	@GetMapping("/type/{type}")
	@Cacheable(value = "basDictionary_details", key = "#type")
	public R getDictByType(@PathVariable String type) {
		return new R<>(basDictionaryService.list(Wrappers
				.<BasDictionary>query().lambda()
				.eq(BasDictionary::getType, type)));
	}

	/**
	 * 添加字典
	 *
	 * @param basDictionary 字典信息
	 * @return success、false
	 */
	@SysLog("添加字典")
	@PostMapping
	@CacheEvict(value = "basDictionary_details", key = "#basDictionary.type")
	@PreAuthorize("@pms.hasPermission('bas_basDictionary_add')")
	public R save(@Valid @RequestBody BasDictionary basDictionary) {
		return new R<>(basDictionaryService.save(basDictionary));
	}

	/**
	 * 删除字典，并且清除字典缓存
	 *
	 * @param id   ID
	 * @param type 类型
	 * @return R
	 */
	@SysLog("删除字典")
	@DeleteMapping("/{id}/{type}")
	@CacheEvict(value = "basDictionary_details", key = "#type")
	@PreAuthorize("@pms.hasPermission('bas_basDictionary_del')")
	public R removeById(@PathVariable Integer id, @PathVariable String type) {
		return new R<>(basDictionaryService.removeById(id));
	}

	/**
	 * 修改字典
	 *
	 * @param basDictionary 字典信息
	 * @return success/false
	 */
	@PutMapping
	@SysLog("修改字典")
	@CacheEvict(value = "basDictionary_details", key = "#basDictionary.type")
	@PreAuthorize("@pms.hasPermission('bas_basDictionary_edit')")
	public R updateById(@Valid @RequestBody BasDictionary basDictionary) {
		return new R<>(basDictionaryService.updateById(basDictionary));
	}

	/**
	 * 查询字典表
	 *
	 * @param dto
	 * @return
	 */
	@SysLog("查询字典表")
	@PostMapping("/sel")
	public R sel(@Valid @RequestBody DictionaryDTO dto) {
		return new R<>(basDictionaryService.sel(dto));
	}

	/**
	 * 查询字典label
	 *
	 * @param type  字典类别
	 * @param value 数据值
	 * @return String
	 */
	@PostMapping("/getDic")
	public String getDic(String type, String value) {
		return basDictionaryService.getDic(type, value);
	}

	/**
	 * 查询字典label
	 *
	 * @param type  字典类别
	 * @param value 数据值
	 * @return String
	 */
	@PostMapping("/getDicLabel/{type}/{value}")
	public String getDicLabel(@PathVariable String type, @PathVariable String value) {
		return basDictionaryService.getDic(type, value);
	}

	/**
	 * 重写字典表查询
	 *
	 * @param type 类别
	 * @return
	 */
	@GetMapping("/dicMap")
	public Map<String, String> dicMap(@RequestParam("type") String type) {
		return basDictionaryService.dicMap(type);
	}

	/**
	 * 重写字典表查询
	 *
	 * @param types 类别集合
	 * @return
	 */
	@PostMapping("/getDicMap")
	@Deprecated
	public Map<String, Map<String, String>> getDicMap(@RequestBody List<String> types) {
		return basDictionaryService.getDicMap(types);
	}

}
